package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.sgb.kjwl.model.entity.request.LeaseAskFilterJson;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.machine.AskLeaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseFragModel extends BaseViewModel {

    private final AskLeaseFragment askLeaseFragment;
    private final MachineRepertory repertory;
//    public ObservableField<MatchEntity> matchEntity = new ObservableField<>(new MatchEntity());

    public AskLeaseFragModel(AskLeaseFragment askLeaseF, MachineRepertory repertory) {
        this.askLeaseFragment = askLeaseF;
        this.repertory = repertory;
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    public void getDataList(String categoryType, String classNo, String typeNo, String provinceNo, String cityNo, String addrNo, String sortNo, int page) {
        if (!TextUtils.isEmpty(categoryType)) {
            //因为数据下标是从0开始的，儿接口数据是从1开始的，需要加1
            categoryType = Integer.parseInt(categoryType) + 1 + "";
        }
        LeaseAskFilterJson filterJson = new LeaseAskFilterJson();
        filterJson.setProvinceNo(provinceNo);
        filterJson.setCityNo(cityNo);
        filterJson.setCurrent(page);
        filterJson.setSortWay(sortNo);
        filterJson.setType(categoryType);
        filterJson.setFirstCategoryNo(classNo);
        filterJson.setSecondCategoryNo(typeNo);
        if (!TextUtils.isEmpty(addrNo)) {
            List<String> addrNos = Arrays.asList(addrNo.split(","));
            filterJson.setAreaNos(addrNos);
        }
        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getAskRentLeaseAll(filterJson).enqueue(new Callback<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseFragment.showList(response.body().getData().getList());
                    } else {
                        MToast.showToast(askLeaseFragment.getActivity(), "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(String categoryType, String id) {
        String urlName = "";
        if ("1".equals(categoryType)) {//机械
            urlName = "mechanicsRentDetails";
        } else if ("2".equals(categoryType)) {//设备
            urlName = "equipmentRentDetails";
        } else if ("3".equals(categoryType)) {//车辆
            urlName = "vehicleRentDetails";
        } else if ("4".equals(categoryType)) {//房产
            urlName = "houseRentDetails";
        } else if ("5".equals(categoryType)) {//土地
            urlName = "landRentDetails";
        } else if ("6".equals(categoryType)) {//工厂
            urlName = "factoryRentDetails";
        } else if ("7".equals(categoryType)) {//农机
            urlName = "farmRentDetails";
        }
        if (TextUtils.isEmpty(urlName)) {
            return;
        }
        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url);
                    WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url, id, "asklease", categoryType);
                } else {
                    MToast.showToast(askLeaseFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//    public void getCityData() {
//        APIManager.getInstance().getMachineAPI().getAreaSecondAndThird2(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    askLeaseFragment.showAddressDatas(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
//
//            }
//        });
//    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    askLeaseFragment.showAddressDatas(data, repertory.getAreaCode());
                }
            }
        });
    }
}
